﻿using AppFx.BizTalk.Dynamics.CRM.Helpers.CRMDataExtract;
using AppFx.BizTalk.IO.Streams;
using log4net.Config;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppFx.BizTalk.Dynamics.CRM.Tests.Helpers.CRMDataExtract
{
    [TestClass]
    public class CRMDownloadManagerTests
    {
        /// <summary>
        /// Dont have a crm instance to run this all of the time
        /// </summary>        
        [TestMethod]
        [Ignore]
        public void CRMDownloadManagerTests_GreenPath()
        {
            XmlConfigurator.Configure();

            var fetchXmlPath = @"..\..\..\AppFx.BizTalk.Dynamics.CRM.Tests\Helpers\CRMDataExtract\FetchXmlSample.xml";
            var crmConnectionString = "TBC";
            var xmlWrapperEnd = "</ns0:Extract>";
            var modifiedXmlStart = "<ns0:Extract xmlns:ns0=\"http://acme\" extractName=\"Extract\" messageId=\"%messageid%\" extractDate=\"%date-yyyyMMdd%\">";
            
            var fetchXmlRequest = File.ReadAllText(fetchXmlPath);
            var crmWrapper = new CRMDownloadManager(crmConnectionString, fetchXmlRequest)
            {
                MessageEnd = xmlWrapperEnd,
                MessageStart = modifiedXmlStart,
                FetchCount = 2000
            };

            var crmDownloadStream = new GenericDownloadStream(crmWrapper);
            using (var outputStream = new FileStream("CRMDownloadManagerTests.GreenPath.xml", FileMode.Create, FileAccess.Write))
            {
                crmDownloadStream.CopyTo(outputStream);
            }
        }
    }
}
